package org.ling.xuan.action;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.config.TestDbConfig;
import org.ling.xuan.dao.SettingDao;
import org.ling.xuan.dialog.SettingDialog;
import org.ling.xuan.entity.mo.Setting;
import org.ling.xuan.util.LoggerUtil;
import org.ling.xuan.util.LxOptional;


/**
 * 弹出设置面板
 *
 * @author liting50684
 * @date 2023/08/21
 */
public class LxSettingAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getProject();
        String basePath = LxOptional.of(e, c -> c.getProject().getBasePath()).orElse("");
        Setting setting = null;
        if (StrUtil.isNotBlank(basePath)) {
            setting = SettingDao.getSetting(e.getProject(), basePath);
            SettingDialog settingDialog = new SettingDialog(project, true, setting);
            if (settingDialog.showAndGet()) {
                SettingDao.updateSetting(basePath, settingDialog.getSetting());
                TestDbConfig.refresh();
                LoggerUtil.log(e.getProject(), "系统设置保存成功");
            }
        }

    }
}
